<?php
include_once 'application/controllers/AbstractController.php';
require_once 'application/models/Menu.php';
require_once 'application/models/MenuPrincipal.php';
require_once 'application/models/SousMenu.php';
require_once 'application/models/Utilisateur.php';

class SousMenuController extends AbstractController 
{
	private static $_VISIBILITY_PUBLIC_READ = 0;
	private static $_VISIBILITY_PUBLIC_WRITE = 1;
	private static $_VISIBILITY_PRIVATE = 2;
	
	function indexAction()
    {
		parent::indexAction();
    	HttpUtil::do405();
    }
    
    
    function getAction()
    {
		parent::getAction();
    	HttpUtil::do405();
    }
    
    function postAction()
    {
		parent::postAction();
		$name = $this->getRequest()->getParam('nom');
		
		$isValid = TypeChecker::validate(array(
			array('data' => $name, 'type' => TypeChecker::$_TEXT_TYPE, 'name' => 'nom'),
		));
		if ($isValid !== true) {
			HttpUtil::do400('validation', $isValid);
		}
	
		$menuId = $this->getRequest()->getParam('menu_principal_id');
		$table = new SousMenu();
		
		// get sort_order
		$whereSortOrder = $table->getAdapter()->quoteInto('menu_principal_id = ?', $menuId);
		$orderby = array('sort_order desc');
	    $menuRows = $table->fetchAll($whereSortOrder, $orderby, 1);
	    $sortOrder = 1;
	    if (isset($menuRows[0])) {
	    	$sortOrder = $menuRows[0]['sort_order'] + 1;
	    }
		
		// check rights
		$tableMp = new MenuPrincipal();
		$where = array();
		$where[] = $tableMp->getAdapter()->quoteInto('menu_id = ?', $this->user->private_menu_id);
		$where[] = $tableMp->getAdapter()->quoteInto('id = ?', $menuId);
	    $menuRows = $tableMp->fetchAll($where);
	    if (!isset($menuRows[0])) {
	    	HttpUtil::do401();
	    }
	    
		// insert data
		try {
			$data = array(
				'nom' => $name,
				'sort_order' => $sortOrder,
				'menu_principal_id' => $menuId,
				'params' => ''
			);
			 
			$lastInsertId = $table->insert($data);	    
		}catch(Exception $exception) {
			HttpUtil::do400('creation_failed');
		}
			
   		$this->view->data = json_encode(array(
   			'id' => $lastInsertId,
   			'sort_order' => $sortOrder,
   			'menu_id' => $menuId,
   			'nom' => $name,
			'params' => '',
   			'menu_principal_id' => $menuId
   		));
    }
    
    function putAction()
    {
		parent::putAction();
    	//TODO: check rights pour chaque fonction
		
		// get params
		$subMenuId = $this->getRequest()->getParam('subMenuId');
		$batteries = $this->getRequest()->getParam('batteries');
		
		if (!isset($batteries)) {
			HttpUtil::do400();
		}
		
		$batteriesArray = explode (',', $batteries);
		
		$params = 'ids='.$batteries;
		
		// check params type
		$isValid = TypeChecker::validate(array(
			array('data' => $subMenuId, 'type' => TypeChecker::$_NUM_TYPE, 'name' => 'subMenuId')
		));
		if ($isValid !== true) {
			HttpUtil::do400('validation', $isValid);
		}
		
		// check rights
		$tableSm = new SousMenu();
		$where = $tableSm->getAdapter()->quoteInto('id = ?', $subMenuId);
	    $smRows = $tableSm->fetchAll($where);
	    if (!isset($smRows[0])) {
	    	HttpUtil::do400();
	    }else{
	    	$menu_principal_id = $smRows[0]->menu_principal_id;
	    }
		$tableMp = new MenuPrincipal();
		$where = array();
		$where[] = $tableMp->getAdapter()->quoteInto('menu_id = ?', $this->user->private_menu_id);
		$where[] = $tableMp->getAdapter()->quoteInto('id = ?', $menu_principal_id);
	    $menuRows = $tableMp->fetchAll($where);
	    if (!isset($menuRows[0])) {
	    	HttpUtil::do401();
	    }
		
		// update table
		try {
		
			$table = new SousMenu();
			 
			$data = array(
				'params'=> $params
			);
			 
			$where = $table->getAdapter()->quoteInto('id = ?', $subMenuId);

			$numberOfRows = $table->update($data, $where);

		}catch(Exception $exception) {
			HttpUtil::do400('update_failed');
		}
		
	    //return 200
	    $this->_helper->json($data);
	}
    
    function deleteAction()
    {
		parent::deleteAction();
    	HttpUtil::do405();
    }
}
